package com.ruge._08_springboot_springdata_jpa.controller._03_mapping._06_extends;

import com.ruge._08_springboot_springdata_jpa.Application;
import com.ruge._08_springboot_springdata_jpa.controller._03_mapping._03_manyToOne.ManyToOneDeptModel;
import com.ruge._08_springboot_springdata_jpa.controller._03_mapping._03_manyToOne.ManyToOneEmpModel;
import com.ruge._08_springboot_springdata_jpa.controller._03_mapping._04_manyToMany.ManyToManyStuModel;
import com.ruge._08_springboot_springdata_jpa.controller._03_mapping._04_manyToMany.ManyToManyTeacherModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建人 ：爱丽丝、如歌
 * 创建时间 ：2019-07-05  22:43
 * 描述 ：继承的测试
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)//这里是启动类
public class TestExtends {
    @Autowired
    private EntityManager entityManager;

    @Test
    @Transactional
    public void testSave() {
        ExtendsBook book = new ExtendsBook("001");
        ExtendsCloth cloth = new ExtendsCloth("red");
        ExtendsProduct product = new ExtendsProduct("衣服");

        entityManager.persist(book);
        entityManager.persist(cloth);
        entityManager.persist(product);
        entityManager.flush();
    }

    @Test
    @Transactional
    public void testUpdate() {
        ManyToOneDeptModel classModel =   entityManager.find(ManyToOneDeptModel.class,1);
        ManyToOneEmpModel stuModel1 =   entityManager.find(ManyToOneEmpModel.class,2);
        ManyToOneEmpModel stuModel2 =   entityManager.find(ManyToOneEmpModel.class,3);
        ManyToOneEmpModel stuModel3 =   entityManager.find(ManyToOneEmpModel.class,4);
        stuModel1.setName("张三1");
        stuModel2.setName("李四2");
        stuModel3.setName("王五3");
        classModel.setName("三年三班");
        stuModel1.setDepartment(classModel);
        stuModel2.setDepartment(classModel);
        stuModel3.setDepartment(classModel);
        entityManager.merge(stuModel1);
        entityManager.merge(stuModel2);
        entityManager.merge(stuModel3);
        entityManager.merge(classModel);
        entityManager.flush();
    }

    @Test
    @Transactional
    public void testDelete() {
        ManyToOneDeptModel stuModel1 =   entityManager.find(ManyToOneDeptModel.class,1);
        entityManager.remove(stuModel1);
        entityManager.flush();
    }

    @Test
    @Transactional
    public void testSelect() {
       List list = entityManager.createQuery("select  t from ManyToOneDeptModel t").getResultList();
       list.forEach(System.out::println);
    }
}
